<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\UserAuth;
use App\Models\Message;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class UserAuthController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserAuth(['user:id,avatar,nickname']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('name');
            $grid->column('sex')
                 ->using([1 => '男',2 => '女'])
                 ->label([1 => 'primary',2 => 'success']);
            $grid->column('audit_status')
                ->using([1 => '待审核',2 =>'已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserAuth(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('sex');
            $show->field('idcard_reverse_photo');
            $show->field('idcard_front_photo');
            $show->field('audit_status');
            $show->field('reject_reason');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserAuth(), function (Form $form) {
            $form->display('id');
            $form->selectTable('user_id','用户')
                ->from(UserTable::make())
                ->model(User::class,'id','nickname')
                ->required();
            $form->text('name')
                 ->maxLength(60)
                 ->required();
            $form->radio('sex')
                 ->options([1 => '男',2 => '女'])
                 ->required();
            $form->image('idcard_reverse_photo')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->required();
            $form->image('idcard_front_photo')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->required();
            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->when(3,function (Form $form){
                    $form->textarea('reject_reason','拒绝原因')
                        ->saving(function ($value){
                            return (string)$value;
                        })->rules('max:255',['max' => '最多500字']);
                })->required();

            $form->saved(function (Form $form){
                if($form->input('audit_status')==2){
                    DB::table('users')->where('id',$form->input('user_id'))->update(['auth' =>1]);
//                    Message::query()->create([
//                        'type' => 1,
//                        'content' => ''
//                    ]);
                }

                if ($form->input('user_id') && $form->input('audit_status')==2){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '恭喜您申请的个人认证已通过审核'
                    ]));
                }

                if ($form->input('user_id') && $form->input('audit_status')==3){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '您申请的个人认证未通过审核'.$form->input('reject_reason')
                    ]));
                }
            });


            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
